package com.jwaoo.mobile.view.explore;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.dx.as.TabTools;
import com.dx.as.fragment.TFragment;
import com.jwaoo.mobile.R;
import com.jwaoo.mobile.view.explore.adapter.UserRecyclerViewAdapter;
import com.jwaoo.mobile.view.explore.bean.UserBean;
import com.jwaoo.mobile.view.explore.presenter.UserPresenter;
import com.jwaoo.mobile.view.explore.presenter.IUserView;
import com.jwaoo.mobile.view.user.UserActivity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserFragment extends TFragment implements IUserView {

    private final static String TAG = UserFragment.class.getSimpleName();
    private static final String ARG_COLUMN_COUNT = "column-count";
    private UserPresenter presenter;
    private RecyclerView recyclerView;
    private TabTools tabTools;
    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public UserFragment() {
    }

    @Override
    public void initView() {
    }

    @Override
    public int setLayoutI() {
        return 0;
    }

    @Override
    public void dataOpion() {

    }

    @Override
    public void initRpo() {

    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static UserFragment newInstance(int columnCount) {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new UserPresenter(getActivity(),this);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contacts_list, container, false);
        if (tabTools == null) {
            tabTools = new TabTools(onTabClickListener);
            tabTools.initView(view,new String[]{getResources().getString(R.string.tab_online),getResources().getString(R.string.tab_nearby),getResources().getString(R.string.tab_ranking)});
        }
        recyclerView = (RecyclerView) view.findViewById(R.id.list);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (recyclerView.getAdapter() == null || recyclerView.getAdapter().getItemCount() == 0){
            Map<String,Object> map = new HashMap<>();
//            map.put("longitude",121.4179104567);
//            map.put("latitude",31.176031387);
            presenter.getNearByList(map);
        }
    }

    @Override
    public void getData(final List<UserBean> list) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                recyclerView.setAdapter(new UserRecyclerViewAdapter(list, mListener));
            }
        });
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
    public interface OnListFragmentInteractionListener {
        void onListFragmentInteraction(UserBean item);
    }
    private OnListFragmentInteractionListener mListener = new OnListFragmentInteractionListener() {
        @Override
        public void onListFragmentInteraction(UserBean item) {
//            IFroChatMouleImpl.getInstance().startChatting(item.getUid());
            Intent intent = new Intent(getActivity(), UserActivity.class);
            intent.putExtra("id",Integer.parseInt(item.getId()));
            startActivity(intent);
        }
    };

    private TabTools.OnTabClickListener onTabClickListener = new TabTools.OnTabClickListener() {
        @Override
        public void onTabClick(int option) {

        }
    };
}
